package top.dprkshow.practice.graph;

import edu.princeton.cs.algs4.Bag;

/**
 * @author Kent HAN
 * @date 2021/9/15
 */
public class WeightedUgraph {
    private int totalVertices;
    private Bag<Edge>[] adjBagList;

    public WeightedUgraph(int totalVertices) {
        this.totalVertices = totalVertices;
        adjBagList = (Bag<Edge>[]) new Bag[totalVertices];
        for (int i = 0; i < totalVertices; i++) {
            adjBagList[i] = new Bag<>();
        }
    }

    public void addEdge(Edge e) {
        int v = e.either(), w = e.other(v);
        adjBagList[v].add(e);
        adjBagList[w].add(e);
    }

    public Iterable<Edge> adj(int vertex) {
        return adjBagList[vertex];
    }

    public int getTotalVertices() {
        return totalVertices;
    }
}
